package edu.fsu.cs.SHPE;

import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.drawable.Drawable;
import android.util.Log;

public class ConferenceContainer extends ArrayList<Conference>{

	private static final long serialVersionUID = -7193563726942777406L;
	private Conference tmpConference;
	private String tmpString;
	private static final String TAG = "SHPE.ConferenceContainer";
	
	public ConferenceContainer(String url) throws ConnectException{
		if(!parseXml(url))
			throw new ConnectException();
	}
	
	public Conference findById(int id){
		for(Conference c : this){
			if(c.getId() == id)
				return c;
		}
		
		return null;
	}
	

	private boolean parseXml(String url){

		Log.i(TAG, "Parse <" + url + ">");

		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			URL sourceUrl = new URL(url);
			ConferenceXMLHandler conferenceHandler = new ConferenceXMLHandler();
			xr.setContentHandler(conferenceHandler);
			xr.parse(new InputSource(sourceUrl.openStream()));	

		} catch (Exception e) {
			return false;
		}
		
		return true;

	}

	public class ConferenceXMLHandler extends DefaultHandler {


		public void startDocument() throws SAXException {
			Log.i(TAG, "Document processing started");

			tmpConference = null;
		}


		public void endDocument() throws SAXException {
			Log.i(TAG, "Document processing finished");

			if(tmpConference != null)
				add(tmpConference);
		}


		public void startElement(String uri, String localName, String qName, Attributes attributes){
			Log.i(TAG, "in element " + localName + " qName " + qName + " uri " + uri);


			if(localName.equalsIgnoreCase("conference-venue") || localName.equalsIgnoreCase("conference") || 
					localName.equalsIgnoreCase("conference-session") || localName.equalsIgnoreCase("conference-speaker") || 
					localName.equalsIgnoreCase("conference-track")){


				if(tmpConference != null){
					add(tmpConference);
				}


				tmpConference = null;

				tmpConference = new Conference();

				if(attributes.getValue("name") != null)
					tmpConference.setName(attributes.getValue("name"));

				if(attributes.getValue("title") != null)
					tmpConference.setName(attributes.getValue("title"));

				if(attributes.getValue("first") != null)
					tmpConference.setName(attributes.getValue("first") + " " + attributes.getValue("last"));

				if(attributes.getValue("xlink:photo") != null){
					URL src;
					try {
						src = new URL(attributes.getValue("xlink:photo"));
						tmpConference.setImage(Drawable.createFromStream(src.openStream(), "photo"));
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				if(attributes.getValue("xlink:thumbnail") != null){
					URL src;
					try {
						src = new URL(attributes.getValue("xlink:thumbnail"));
						tmpConference.setImage(Drawable.createFromStream(src.openStream(), "photo"));
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				if(attributes.getValue("xlink:logo") != null){
					URL src;
					try {
						src = new URL(attributes.getValue("xlink:logo"));
						tmpConference.setImage(Drawable.createFromStream(src.openStream(), "photo"));
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				tmpConference.setId(Long.parseLong(attributes.getValue("id")));

			}



			if(attributes.getValue("first") != null && attributes.getValue("last") != null)
				tmpConference.setName(attributes.getValue("first") + " " + attributes.getValue("last"));

			if(localName.equalsIgnoreCase("coordinate")){
				tmpConference.setLat(attributes.getValue("lat"));
				tmpConference.setLon(attributes.getValue("lon"));
			}
			if(localName.equalsIgnoreCase("location")){
				tmpConference.setRoom(attributes.getValue("room"));
			}

		}

		public void characters(char[] ch, int start, int length) throws SAXException{
			tmpString = new String(ch, start, length);
			Log.i(TAG, "string " + tmpString);
		}

		public void endElement(String uri, String localName, String qName)
		throws SAXException {
			Log.i(TAG, "endElement " + localName);

			if(localName == null || tmpString == null || tmpConference == null)
				return;


			if(localName.equalsIgnoreCase("description"))
				tmpConference.setDescription(tmpString);
			else if(localName.equalsIgnoreCase("email"))
				tmpConference.setEmail(tmpString);
			else if(localName.equalsIgnoreCase("website"))
				tmpConference.setWebsite(tmpString);
			else if(localName.equalsIgnoreCase("start-date") || localName.equalsIgnoreCase("start-time"))
				tmpConference.setStartDate(tmpString);
			else if(localName.equalsIgnoreCase("end-date") || localName.equalsIgnoreCase("end-time"))
				tmpConference.setEndDate(tmpString);
			else if(localName.equalsIgnoreCase("address"))
				tmpConference.setAddress(tmpString);
			else if(localName.equalsIgnoreCase("city"))
				tmpConference.setCity(tmpString);
			else if(localName.equalsIgnoreCase("province"))
				tmpConference.setProvince(tmpString);
			else if(localName.equalsIgnoreCase("country"))
				tmpConference.setCountry(tmpString);
			else if(localName.equalsIgnoreCase("postalCode"))
				tmpConference.setPostalCode(tmpString);	
			else if(localName.equalsIgnoreCase("phone"))
				tmpConference.setPhone(tmpString);
			else if(localName.equalsIgnoreCase("title"))
				tmpConference.setTitle(tmpString);
			else if(localName.equalsIgnoreCase("organizationOrCompany"))
				tmpConference.setOrganization(tmpString);
			else if(localName.equalsIgnoreCase("linkedIn"))
				tmpConference.setLinkedIn(tmpString);
			else if(localName.equalsIgnoreCase("biography"))
				tmpConference.setBio(tmpString);
			else if(localName.equalsIgnoreCase("twitter"))
				tmpConference.setTwitter(tmpString);
				
		}

	}
}